# -*- coding: utf-8  -*-
#!/usr/local/bin/python

__author__ = 'jian.yao'

from django.db import  models
from django.utils import timezone
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager

class UserGenderType:
    Unknown = 0
    Male    = 1
    Female  = 2

class UserContactType:
    Unknown     = 0
    Email       = 10
    Mobile1     = 20
    Mobile2     = 21
    Mobile3     = 22

class UserManager(BaseUserManager):
    def create_user(self, channel, username, password, **extra_fields):
        now = timezone.now()
        if not username:
            raise ValueError('The given username must be set')
        user = self.model(username = username,
                          is_staff = False,
                          is_active = True,
                          is_superuser = False,
                          last_login = now,
                          date_joined = now,
                          **extra_fields)
        user.set_password(password)
        user.save(using = self._db)
        return user

    def create_superuser(self, username, password, **extra_fields):
        u = self.create_user(0, username, password, **extra_fields)
        u.is_staff = True
        u.is_active = True
        u.is_superuser = True
        u.save(using = self._db)
        return u

class User(AbstractBaseUser, PermissionsMixin):
    username    = models.CharField(max_length = 100, unique = True)
    nickname    = models.CharField(max_length = 100, blank = True)
    is_staff    = models.BooleanField(default = False)
    is_active   = models.BooleanField(default = True)
    date_joined = models.DateTimeField(default = timezone.now)

    channel     = models.IntegerField(null = False, blank = False, default = 0)
    pin         = models.CharField(max_length=10, null = True, blank=True)
    gender      = models.IntegerField(null = False, blank = False, default = UserGenderType.Unknown)
    adcode      = models.CharField(max_length = 10, null = True, blank = True)
    address     = models.CharField(max_length = 500, null = True, blank = True)
    birthday    = models.DateField(null = True, blank = True)
    device_info = models.CharField(max_length = 100, null = True, blank = True)
    description = models.CharField(max_length = 500, null = True, blank = True)
    last_update = models.DateTimeField(auto_now = True)

    objects = UserManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = []

    class Meta:
        swappable = 'AUTH_USER_MODEL'

class UserContact(models.Model):
    user            = models.ForeignKey(User, null = False, blank = False, db_index = True)
    type            = models.IntegerField(null = False, blank = False, default = UserContactType.Unknown)
    value           = models.CharField(max_length = 200, null = True, blank = True)
    is_confirmed    = models.BooleanField(null = False, blank = False, default = False)
    last_update     = models.DateTimeField(auto_now = True)

class UserProviderType:
    Unknown     = 0
    SinaWeibo   = 1

class UserProvider(models.Model):
    user                = models.ForeignKey(User, null = False, blank = False)
    provider            = models.IntegerField(null = False, blank = False, default = UserProviderType.Unknown)
    auth_id             = models.CharField(max_length = 100, null = True, blank = True)
    auth_username       = models.CharField(max_length = 100, null = True, blank = True)
    auth_token          = models.CharField(max_length = 100, null = True, blank = True)
    auth_token_verifier = models.CharField(max_length = 100, null = True, blank = True)
    description         = models.CharField(max_length = 500, null = True, blank = True)
    createtime          = models.DateTimeField(auto_now_add = True, db_index = True)
    expire_date         = models.DateTimeField(null = True)

    class Meta:
        unique_together = ("provider", "auth_id")
